//
//  GJFVideoCollectViewController.m
//  Wedding
//
//  Created by Marx on 2017/1/14.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFVideoCollectViewController.h"
#import "GJFVideoModel.h"
#import "GJFVideoCollectionViewCell.h"
#import "GJFWebViewController.h"
@interface GJFVideoCollectViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UMSocialShareMenuViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrModel;

@end

@implementation GJFVideoCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"视频收藏";
    [self collectionView];
    [self getVideoData];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-返回"] style:UIBarButtonItemStyleDone target:self action:@selector(popVc)];
}

- (void)popVc {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 视频的网络请求
- (void)getVideoData {
    [GJFNetworkTool POSTRequestWithUrl:APP_MINE_VIDEOCOLLECT params:@{} complete:^(NSDictionary *result) {
        [self.collectionView.mj_header endRefreshing];
        if ([result[@"code"] intValue] == 1000) {
            NSArray *array = result[@"data"];
            self.arrModel = [GJFVideoModel transformWithArr:array];
            [self.collectionView reloadData];
        }
    }];
}
#pragma mark - CollectionView的分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
#pragma mark - CollectionView分区中item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrModel.count;
}
#pragma mark - CollectionView每个分区中item的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GJFVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VideoCell" forIndexPath:indexPath];
    cell.videoModel = self.arrModel[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = @{@"videoId":[self.arrModel[indexPath.item] id]};
    [GJFNetworkTool POSTRequestWithUrl:APP_VIDEO_PLAY params:dict complete:^(NSDictionary *result) {}];
    GJFWebViewController *webVC = [[GJFWebViewController alloc] init];
    GJFVideoModel *videoModel = self.arrModel[indexPath.row];
    [webVC createInfoWithTitle:videoModel.sourcename url:videoModel.url options:nil];
    webVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webVC animated:YES];
}
#pragma mark - 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((WIDTH - 15) / 2, (WIDTH - 15) / 2);
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 0, 5);
        layout.minimumLineSpacing = 5; //行间距
        layout.minimumInteritemSpacing = 0; // 列间距
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor colorWithRed:0.91 green:0.90 blue:0.90 alpha:1.00];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_collectionView];
        
        [self.collectionView registerClass:[GJFVideoCollectionViewCell class] forCellWithReuseIdentifier:@"VideoCell"];
        MJRefreshGifHeader *header = [GJFRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getVideoData)];
        _collectionView.mj_header = header;
    }
    return _collectionView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
